package com.hspedu.homework;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author Stringzhua
 * @Date 2024/3/13 19:11
 * description:
 */
@SuppressWarnings("all")
public class homework02 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        Car car = new Car("宝马", 400000);
        Car car1 = new Car("宾利", 5000000);
        //1.add:添加单个元素
        arrayList.add(car);
        arrayList.add(car1);
        System.out.println("arrayList = " + arrayList);

        //2.remove:删除指定元素
        arrayList.remove(0);
        System.out.println("arrayList = " + arrayList);

        //3.contains:查找元素是否存在
        System.out.println(arrayList.contains(0));//false

        //4.size:获取元素个数
        System.out.println("arrayList.size() = " + arrayList.size());//1

        //5.isEmpty:判断是否为空
        System.out.println("arrayList.isEmpty() = " + arrayList.isEmpty());//false

        //6.clear:清空
//        arrayList.clear();
//        System.out.println("arrayList = " + arrayList);//[]

        //7.addAll:添加多个元素
        System.out.println("arrayList = " + arrayList);
        arrayList.addAll(arrayList);
        System.out.println("arrayList = " + arrayList);
        //8.containsAll:查找多个元素是否都存在
        arrayList.containsAll(arrayList);//T
        //9.removeAll:删除多个元素
//        arrayList.removeAll(arrayList);//相当于清空
        //增强for遍历
        for (Object o : arrayList) {
            System.out.println("o = " + o);
        }

        //迭代器遍历
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("next = " + next);
        }

    }
}

class Car {
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
